from PySide2.QtCore import *
from PySide2.QtWidgets import *
import time

class Signal_t(QObject):
    sig = Signal(str)

class MyThread(QThread):
    def __init__(self):
        super().__init__()
        self.tasks = []
        self.sig = Signal_t()

    def add_task(self, task):
        self.tasks.append(task)

    def run(self):
        i = 0
        while True :#self.tasks != []:
            # task = self.tasks.pop(0)
            # self.tasks[0]()  # 执行任务
            print(time.time(),i)
            i += 1
            self.sig.sig.emit(str(i))
            # time.sleep(1)

class MyApp(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()
        
    
    def init_ui(self):
        self.centralwidget = QWidget(self)
        self.centralwidget.setObjectName(u"centralwidget")
        self.setCentralWidget(self.centralwidget)

        layout = QVBoxLayout(self.centralwidget)
        self.setLayout(layout)

        self.line_edit = QLineEdit('哈哈')
        self.button = QPushButton("Trigger task")
        self.button.clicked.connect(self.handle_trigger)
        layout.addWidget(self.button)
        layout.addWidget(self.line_edit)
        


    def handle_trigger(self):
        self.my_thread = MyThread()
        self.my_thread.sig.sig.connect(self.show_test)
        # self.my_thread.add_task(self.handle_task)
        self.my_thread.start()
        # self.my_thread.moveToThread(QApplication.instance().thread())

    def show_test(self,s:str):
        self.line_edit.setText(s)

    def handle_task(self):
        print("Task executed in thread!",self.xx)

if __name__ == "__main__":
   app = QApplication([])
   window = MyApp()
   window.show()
   app.exec_()